package org.aksw.xoperator.conf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.aksw.xoperator.security.SecurityConfiguration;
import org.aksw.xoperator.sparql.SparqlEndpointIdentifier;
import org.aksw.xoperator.sparql.http.RemoteSparqlEndpointConfiguration;
import org.aksw.xoperator.sparql.local.LocalSparqlEndpointConfiguration;
import org.aksw.xoperator.xmpp.JabberAccount;
import org.aksw.xoperator.xmpp.JabberClientConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Manages the 
 * @author joerg
 *
 */
public class ConfigurationSerializer {
	
	private static Log log = LogFactory.getLog(ConfigurationSerializer.class);
	
	private XStream xstream = new XStream(new DomDriver());
	
	public ConfigurationSerializer(String storageDirectory) {
		this.storageDirectory = storageDirectory;
		xstream.alias("account", JabberAccount.class);
		xstream.alias("agent",AgentConfiguration.class);
		xstream.alias("jabber", JabberClientConfiguration.class);
		xstream.alias("localStore",LocalSparqlEndpointConfiguration.class);
		xstream.alias("httpEndpoints",RemoteSparqlEndpointConfiguration.class);
		xstream.alias("endpointId", SparqlEndpointIdentifier.class);
		xstream.alias("httpEndpoint",RemoteSparqlEndpointConfiguration.SparqlEndpointConfigurationItem.class);
		xstream.alias("security",SecurityConfiguration.class);
	}

	private String storageDirectory;
	
	


	public AgentConfiguration load(){
		File confFile = new File(storageDirectory + "/agent.conf.xml");
		//File aimlFile = new File(storageDirectory + "/aiml/templates/user.aiml");
		AgentConfiguration aconf = null;
		try {
			aconf = (AgentConfiguration) xstream.fromXML(new FileReader(confFile));
			//aconf.setTemplateConfiguration((TemplateConfiguration) xstream.fromXML(new FileReader(aimlFile)));
			aconf.setConfDir(storageDirectory);
			
		} catch (FileNotFoundException e) {
			log.error("Unable to read configuration from file ",e );
		}
		
		
		
		
		return aconf;
	}
	
	public void save(AgentConfiguration agentConf){
		//serialize data into the configuration file
		
		
		try {
			File confFile = new File(storageDirectory + "/agent.conf.xml");
			confFile.getParentFile().mkdirs();
			xstream.toXML(agentConf, new FileWriter(confFile));
			log.info("Wrote config to: " + new File(storageDirectory + "/agent.conf.xml").getAbsolutePath());
		} catch (IOException e) {
			log.error("Error writing configuration to disk: ", e);
		}
		
		//serialize the aiml configuration
		
//		try {
//			File aimlFile = new File(storageDirectory + "/aiml/templates/user.aiml");
//			aimlFile.getParentFile().mkdirs();
//			xstream.toXML(agentConf.getTemplateConfiguration(), new FileWriter(aimlFile));
//		} catch (IOException e) {
//			log.error("Error writing aiml configuration to disk: ", e);
//		}
		
		
	}


}
